import warnings
from django.utils.encoding import force_str
from rest_framework import RemovedInDRF317Warning
from django.utils.translation import gettext_lazy as _
from django.http import HttpResponse

from django.shortcuts import render
import json
from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import status
from django.conf import settings
import os


# Create your views here.
from .serializers import *
from rest_framework import viewsets
from .models import *


from rest_framework.pagination import PageNumberPagination
from rest_framework.decorators import action

# Create your views here.
from django_filters.rest_framework import DjangoFilterBackend, CharFilter

from django_filters import FilterSet, DateFilter

from django.utils.timezone import make_aware
from datetime import datetime, timedelta

from django.http import HttpResponse
from datetime import datetime

# views.py
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from rest_framework.schemas import AutoSchema, coreapi
import coreschema

# from rest_framework.schemas.openapi import AutoSchema
from rest_framework import serializers
from coreapi import Field
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

from django.db.models import F

# Create your views here.


class DataPartcountView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            # Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(
                name="assetnum",
                required=False,
                location="query",
                schema=coreschema.String(description="设备编号"),
            ),
            Field(
                name="endpointnum",
                required=False,
                location="query",
                schema=coreschema.String(description="设备端点编号"),
            ),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = DataPartcountViewSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        queryset = DataPartcount.objects.using("postgresql").filter().order_by("timestamp")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)
        print(start_time_param)
        print(end_time_param)
        # id = request.query_params.get("id")
        assetnum = request.query_params.get("assetnum")
        # 根据参数是否存在添加不同筛选条件
        # if id is not None:
        #     queryset = queryset.filter(id=id)
        if assetnum is not None:
            queryset = queryset.filter(assetnum__contains=assetnum)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(timestamp__gte=start_time_param_trans, timestamp__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class DataCycletimeView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            # Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(
                name="assetnum",
                required=False,
                location="query",
                schema=coreschema.String(description="设备编号"),
            ),
            Field(
                name="endpointnum",
                required=False,
                location="query",
                schema=coreschema.String(description="设备端点编号"),
            ),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = DataCycletimeViewSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        queryset = DataCycletime.objects.using("postgresql").filter().order_by("timestamp")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)
        print(start_time_param)
        print(end_time_param)
        # id = request.query_params.get("id")
        assetnum = request.query_params.get("assetnum")
        # 根据参数是否存在添加不同筛选条件
        # if id is not None:
        #     queryset = queryset.filter(id=id)
        if assetnum is not None:
            queryset = queryset.filter(assetnum__contains=assetnum)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(timestamp__gte=start_time_param_trans, timestamp__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class DataFaultcodeView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            Field(name="fault_id", required=False, location="query", schema=coreschema.Number(description="fault_id")),
            Field(
                name="assetnum",
                required=False,
                location="query",
                schema=coreschema.String(description="设备编号"),
            ),
            Field(
                name="endpointnum",
                required=False,
                location="query",
                schema=coreschema.String(description="设备端点编号"),
            ),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = DataFaultcodeViewSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        queryset = DataFaultcode.objects.using("postgresql").filter().order_by("timestamp")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)
        print(start_time_param)
        print(end_time_param)
        # id = request.query_params.get("id")
        assetnum = request.query_params.get("assetnum")
        # 根据参数是否存在添加不同筛选条件
        # if id is not None:
        #     queryset = queryset.filter(id=id)
        if assetnum is not None:
            queryset = queryset.filter(assetnum__contains=assetnum)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(timestamp__gte=start_time_param_trans, timestamp__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class DataMachinefaultView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            # Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(
                name="assetnum",
                required=False,
                location="query",
                schema=coreschema.String(description="设备编号"),
            ),
            Field(
                name="endpointnum",
                required=False,
                location="query",
                schema=coreschema.String(description="设备端点编号"),
            ),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = DataMachinefaultViewSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        queryset = DataMachinefault.objects.using("postgresql").filter().order_by("createdate")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)
        print(start_time_param)
        print(end_time_param)
        # id = request.query_params.get("id")
        assetnum = request.query_params.get("assetnum")
        # 根据参数是否存在添加不同筛选条件
        # if id is not None:
        #     queryset = queryset.filter(id=id)
        if assetnum is not None:
            queryset = queryset.filter(assetnum__contains=assetnum)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(createdate__gte=start_time_param_trans, createdate__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class DataShiftcountView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            # Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(
                name="assetnum",
                required=False,
                location="query",
                schema=coreschema.String(description="设备编号"),
            ),
            Field(
                name="endpointnum",
                required=False,
                location="query",
                schema=coreschema.String(description="设备端点编号"),
            ),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = DataShiftcountViewSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        queryset = DataShiftcount.objects.using("postgresql").filter().order_by("timestamp")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)
        print(start_time_param)
        print(end_time_param)
        # id = request.query_params.get("id")
        assetnum = request.query_params.get("assetnum")
        # 根据参数是否存在添加不同筛选条件
        # if id is not None:
        #     queryset = queryset.filter(id=id)
        if assetnum is not None:
            queryset = queryset.filter(assetnum__contains=assetnum)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(timestamp__gte=start_time_param_trans, timestamp__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class DataTooldoortimeView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            # Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(
                name="assetnum",
                required=False,
                location="query",
                schema=coreschema.String(description="设备编号"),
            ),
            Field(
                name="endpointnum",
                required=False,
                location="query",
                schema=coreschema.String(description="设备端点编号"),
            ),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = DataTooldoortimeViewSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        queryset = DataTooldoortime.objects.using("postgresql").filter().order_by("timestamp")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)
        print(start_time_param)
        print(end_time_param)
        # id = request.query_params.get("id")
        assetnum = request.query_params.get("assetnum")
        # 根据参数是否存在添加不同筛选条件
        # if id is not None:
        #     queryset = queryset.filter(id=id)
        if assetnum is not None:
            queryset = queryset.filter(assetnum__contains=assetnum)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(timestamp__gte=start_time_param_trans, timestamp__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class SummaryCycletimeView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            # Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(
                name="assetnum",
                required=False,
                location="query",
                schema=coreschema.String(description="设备编号"),
            ),
            Field(
                name="endpointnum",
                required=False,
                location="query",
                schema=coreschema.String(description="设备端点编号"),
            ),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = SummaryCycletimeViewSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        queryset = SummaryCycletime.objects.using("postgresql").filter().order_by("createdate")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)
        print(start_time_param)
        print(end_time_param)
        # id = request.query_params.get("id")
        assetnum = request.query_params.get("assetnum")
        # 根据参数是否存在添加不同筛选条件
        # if id is not None:
        #     queryset = queryset.filter(id=id)
        if assetnum is not None:
            queryset = queryset.filter(assetnum__contains=assetnum)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(createdate__gte=start_time_param_trans, createdate__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class SummaryPartcountView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            # Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(
                name="assetnum",
                required=False,
                location="query",
                schema=coreschema.String(description="设备编号"),
            ),
            Field(
                name="day",
                required=False,
                location="query",
                schema=coreschema.String(description="格式示例：2024-07-01"),
            ),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = SummaryPartcountViewSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        queryset = SummaryPartcount.objects.using("postgresql").filter().order_by("createdate")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)
        day = request.query_params.get("day", None)
        print(start_time_param)
        print(end_time_param)
        day = request.query_params.get("day")
        assetnum = request.query_params.get("assetnum")
        # 根据参数是否存在添加不同筛选条件
        if day is not None:
            queryset = queryset.filter(day=day)
        if assetnum is not None:
            queryset = queryset.filter(assetnum__contains=assetnum)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(createdate__gte=start_time_param_trans, createdate__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class ToolChangTimeView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            # Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(
                name="assetnum",
                required=False,
                location="query",
                schema=coreschema.String(description="设备编号"),
            ),
            Field(
                name="toolnumber",
                required=False,
                location="query",
                schema=coreschema.Number(description="刀具编号"),
            ),
            Field(
                name="location",
                required=False,
                location="query",
                schema=coreschema.String(description="生产模块"),
            ),
            Field(
                name="op",
                required=False,
                location="query",
                schema=coreschema.String(description="工位号"),
            ),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = ToolChangTimeViewSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        queryset = ToolChangTime.objects.using("postgresql").filter().order_by("starttime")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)
        print(start_time_param)
        print(end_time_param)
        # id = request.query_params.get("id")
        assetnum = request.query_params.get("assetnum")
        toolnumber = request.query_params.get("toolnumber")
        location = request.query_params.get("location")
        op = request.query_params.get("op")
        # 根据参数是否存在添加不同筛选条件
        if op is not None:
            queryset = queryset.filter(op=op)
        if toolnumber is not None:
            queryset = queryset.filter(toolnumber=toolnumber)
        if location is not None:
            queryset = queryset.filter(location=location)
        if assetnum is not None:
            queryset = queryset.filter(assetnum__contains=assetnum)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(starttime__gte=start_time_param_trans, starttime__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset
